import {queryReader,login} from "@/api";

const state = {
    loginReader:[],
    readerList:[],
    addStatus:{status: false},
    token: '',
    userInfo: JSON.parse(sessionStorage.getItem("userInfo"))
};
const mutations = {
    QUERYREADER(state,readerList){
        state.readerList=readerList;
    },
    ADDREADER(state){
        state.addStatus.status=true;
    },
    CLEARREADERLIST(state){
        state.readerList=[]
    },
    REPORTLOSS(state){
        state.readerList[0].rdStatus="挂失"
    },
    // set
    SET_TOKEN: (state, token) => {
        state.token = token
        sessionStorage.setItem("token", token)
    },
    SET_USERINFO: (state, userInfo) => {
        state.userInfo = userInfo
        sessionStorage.setItem("userInfo", JSON.stringify(userInfo))
    },
    REMOVE_INFO: (state) => {
        state.token = ''
        state.userInfo = {}
        sessionStorage.setItem("token", '')
        sessionStorage.setItem("userInfo", JSON.stringify(''))
    },
    LOGIN(state,loginReader){
        state.loginReader=loginReader;
        sessionStorage.setItem("username",loginReader.username)
    },

};
const actions = {
    async queryReader({commit},params={}){
        let response = await queryReader(params);
        if (response.code==200){
            commit("QUERYREADER",response.data)
        }
    },
    async addReader({commit},params={}){
        let response = await login(params);
        if (response.code==200){
            commit("LOGIN",response.data)
        }
    },
    clearResultList({commit}){
        commit("CLEARREADERLIST")
    },
    reportLoss({commit}){
        commit("REPORTLOSS")
    },

};
const getters = {
    readerList(state){
        return state.readerList[0]
    }
};

export default {
    state,
    mutations,
    actions,
    getters
}
